#include "door_bell_mqtt.h"

#define CONFIG_BROKER_URI "ws://broker.emqx.io:8083/mqtt"

static const char *TAG = "my_mqtt";

void door_bell_mqtt_dataHandle(char *data, int len);

static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
    ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32, base, event_id);
    esp_mqtt_event_handle_t event = event_data;
    esp_mqtt_client_handle_t client = event->client;
    int msg_id;
    switch ((esp_mqtt_event_id_t)event_id)
    {
    case MQTT_EVENT_CONNECTED:

        ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
        msg_id = esp_mqtt_client_subscribe(client, "Ming/cmd", 0);

        break;
    case MQTT_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
        break;

    case MQTT_EVENT_SUBSCRIBED:
        ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);

        break;
    case MQTT_EVENT_UNSUBSCRIBED:
        ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_PUBLISHED:
        ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
        break;
    case MQTT_EVENT_DATA:
        // ESP_LOGI(TAG, "MQTT_EVENT_DATA");
        // printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
        // printf("DATA=%.*s\r\n", event->data_len, event->data);
        door_bell_mqtt_dataHandle(event->data, event->data_len);

        break;
    case MQTT_EVENT_ERROR:
        ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
        break;
    default:
        ESP_LOGI(TAG, "Other event id:%d", event->event_id);
        break;
    }
}

void door_bell_mqtt_Init(void)
{
    const esp_mqtt_client_config_t mqtt_cfg = {
        .broker.address.uri = CONFIG_BROKER_URI,
    };

    esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
    /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */
    esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
    esp_mqtt_client_start(client);
}

extern TaskHandle_t mainTask_Handle;
void door_bell_mqtt_dataHandle(char *data, int len)
{
    // 确保不会有上一次数据的残留
    char buffer[len + 1];
    memset(buffer, 0, len + 1);
    memcpy(buffer, data, len);
    // MY_LOGI("data: %s", data);

    // data: {"cmd":"off","type":"video","dir":"esp2client"}
    // 解析JSON数据
    // 1
    cJSON *obj = cJSON_Parse(buffer);
    // 2
    cJSON *cmdItem = cJSON_GetObjectItem(obj, "cmd");
    cJSON *typeItem = cJSON_GetObjectItem(obj, "type");
    cJSON *dirItem = cJSON_GetObjectItem(obj, "dir");
    // 3
    char *cmd = cJSON_GetStringValue(cmdItem);
    char *type = cJSON_GetStringValue(typeItem);
    char *dir = cJSON_GetStringValue(dirItem);

    // MY_LOGI("%s %s %s", cmd, type, dir);

    if (strcmp(cmd, "on") == 0)
    {
        // MY_LOGI("%s %s %s", cmd, type, dir);
        if (strcmp(type, "audio") == 0)
        {
            if (strcmp(dir, "esp2client") == 0)
            {
                // MY_LOGI("%s %s %s", cmd, type, dir);
                xTaskNotify(mainTask_Handle, ON_AUDIO_MASTER, eSetValueWithOverwrite);
            }
            else if (strcmp(dir, "client2esp") == 0)
            {
                xTaskNotify(mainTask_Handle, ON_AUDIO_VISITOR, eSetValueWithOverwrite);
            }
        }
        else if (strcmp(type, "video") == 0)
        {

            xTaskNotify(mainTask_Handle, ON_VIDEO, eSetValueWithOverwrite);
        }
    }
    else if (strcmp(cmd, "off") == 0)
    {
        // MY_LOGI("%s %s %s", cmd, type, dir);
        if (strcmp(type, "audio") == 0)
        {
            if (strcmp(dir, "esp2client") == 0)
            {
                xTaskNotify(mainTask_Handle, OFF_AUDIO_MASTER, eSetValueWithOverwrite);
            }
            else if (strcmp(dir, "client2esp") == 0)
            {
                xTaskNotify(mainTask_Handle, OFF_AUDIO_VISITOR, eSetValueWithOverwrite);
            }
        }
        else if (strcmp(type, "video") == 0)
        {

            xTaskNotify(mainTask_Handle, OFF_VIDEO, eSetValueWithOverwrite);
        }
    }

    // 4
    cJSON_free(obj);
}
